package com.liuwenlin.zookeeper.zkclient.config;

import org.apache.zookeeper.ZooKeeper;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * @author liuwenlin
 * @version v1.0
 * @date 2021/2/1 23:00
 */
public class TestConfig {

    ZooKeeper zk;

    @Before
    public void conn(){
        zk = ZKUtils.getZooKeeperWithRootPath("/testConf");
    }

    @After
    public void close(){
        try {
            zk.close();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getDynamicConfByWatch(){
        WatchCallBack watchCallBack = new WatchCallBack();
        watchCallBack.setZk(zk);
        MyConf myConf = new MyConf();
        watchCallBack.setConf(myConf);

        //阻塞等待,待检查到节点数据后才打开门闩
        watchCallBack.await();

        //1，节点不存在


        //2，节点存在
        while (true){
            if("".equals(myConf.getConf())){
                System.out.println("conf is lost or node has been deleted!!!");
                watchCallBack.await();
            } else {
                System.out.println("conf's content: " + myConf.getConf());
            }

            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
